import 'package:code_text_field/code_text_field.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/material.dart' as Mat;
import 'package:tabbed_view/tabbed_view.dart';

import '../models/file_list_model.dart';
//import 'package:flutter/material.dart';
//import 'package:flutter_simple_treeview/flutter_simple_treeview.dart';
//import 'package:flutter_editor/components/tree_from_json.dart';

// Import the language & theme
import 'package:highlight/languages/dart.dart';
import 'package:flutter_highlight/themes/monokai-sublime.dart';
import 'package:flutter_highlight/themes/tomorrow.dart';

import 'code_editor.dart';

class EditorTabView extends StatefulWidget {
  const EditorTabView({Key? key}) : super(key: key);

  @override
  _EditorTabView createState() => _EditorTabView();
}

class _EditorTabView extends State<EditorTabView> {
  int? currentIndex;
  late List<TabData> tabs;

  @override
  void initState() {
    super.initState();

    FileList.getInstance().addListener(() {
      debugPrint("value notifier is true");
      setState(() {
        _buildTabs();
      });
    }, ['opened_file', 'closed_file']);

    FileList.getInstance().addListener(() {
      debugPrint("value notifier is true");
      int n_index = FileList.getInstance().current_file_index;
      if (n_index != currentIndex) {
        setState(() {
          currentIndex = n_index;
        });
      }
    }, ['current_index_change']);

    tabs = [];
  }

  void _buildTabs() {
    List<String> open_files = FileList.getInstance().open_files;
    tabs = [];

    currentIndex = FileList.getInstance().current_file_index;

    open_files.forEach((element) {
      String target_name = FileList.getInstance().get_file_name(element)!;
      tabs.add(TabData(text: target_name, value: element));
    });
  }

  Widget _buildTabBody(BuildContext context, int index) {
    print('_buildTabBody index: $index');

    String content =
        FileList.getInstance().get_file_content_by_index(index) ?? '';

    var _codeController = CodeController(
        text: content,
        language: dart,
        theme: tomorrowTheme,
        onChange: (content_n) {
          FileList.getInstance().update_content(index, content_n);
        });

    return build_code_editor(SingleChildScrollView(
      controller: ScrollController(),
      child: CodeField(
        minLines: 100,
        controller: _codeController,
        textStyle: TextStyle(fontFamily: 'SourceCode'),
      ),
    ));
  }

  void _handleTabChanged(int? index) {
    if (index == null) {
      return;
    }
    print('_handleTabChanged index: $index');
    print('_handleTabChanged: $currentIndex  $index');
    FileList.getInstance().select_current_file(index);
  }

  void _handleTabClosed(int index, TabData tab) {
    print('_handleTabClosed index: $index');
    FileList.getInstance().closed_by_index(index);
  }

  @override
  Widget build(BuildContext context) {
    TabbedViewController controller2 = TabbedViewController(tabs);
    controller2.selectedIndex = currentIndex;
    TabbedView tabbedView = TabbedView(
        controller: controller2,
        contentBuilder: _buildTabBody,
        onTabClose: _handleTabClosed,
        onTabSelection: _handleTabChanged,
        closeButtonTooltip: 'Click here to close the tab');
    return Flexible(child: tabbedView);
  }
}
